<template>
	<view class="new-users copy-data">
		<view class="head">
			<view class="user-card">
				<view class="bg"></view>
				<view class="user-info">
					<!-- 注释这个是加的bnt -->
					<!-- #ifdef H5 -->
					<button class="bntImg" v-if="userInfo.is_complete == 0 && isWeixin" @click="getWechatuserinfo">
						<image class="avatar" src='/static/images/f.png'></image>
						<view class="avatarName">获取头像</view>
					</button>
					<!-- #endif -->
					<!-- #ifdef MP -->
					<button class="bntImg" v-if="userInfo.is_complete == 0" open-type='getUserInfo' @getuserinfo="getRoutineUserInfo">
						<image class="avatar" src='/static/images/f.png'></image>
						<view class="avatarName">获取头像</view>
					</button>
					<!-- #endif -->
					<view v-else>
						<image class="avatar" :src='userInfo.avatar' v-if="userInfo.avatar" @click="goEdit()"></image>
						<image v-else class="avatar" src="/static/images/f.png" mode="" @click="goEdit()"></image>
					</view> 
					<view class="info" >
						<!-- #ifdef MP -->
						<view class="name" v-if="!userInfo.uid" @tap="openAuto" style=" z-index: 1000; height: 100%; width: 400rpx;  display: flex; align-items: center;">
							请点击授权
						</view>
						<!-- #endif --> 
						
						<!-- #ifdef H5 -->
						<view class="name" v-if="!userInfo.uid && isWeixin" @tap="openAuto" style=" z-index: 1000; height: 400%; width: 300rpx; display: flex; align-items: center;">
							请点击授权
						</view> 
						<!-- #endif -->
						<view class="name" v-if="userInfo.uid">
							{{userInfo.nickname}}
							<view class="vip" v-if="userInfo.vip">
								<image :src="userInfo.vip_icon" alt="">
									<view style="margin-left: 10rpx;" class="vip-txt">{{userInfo.vip_name}}</view>
							</view>
						</view>
						<view class="num" v-if="userInfo.phone" @click="goEdit()">
							<!-- <view class="num-txt">ID：{{userInfo.uid}}</view> -->
							<view class="num-txt">代理地区: {{mergerName || '暂无地区'}} </view>
							<!-- <view class="icon">
								<image src="/static/images/edit.png" mode=""></image>
							</view> -->
						</view>
						<view class="phone" v-if="!userInfo.phone && isLogin" @tap="bindPhone">绑定手机号</view>
					</view>
					<!-- 地区切换 -->
					<view class="change_area" @click="changeArea" v-show="isShowChangeArea">
						<image class="area_img" src="../../static/images/change.png" mode=""></image>
						<text>切换区域</text>
					</view>

				</view>
				<view class="num-wrapper">
					<view class="num-item" v-if="userInfo.balance_func_status" @click="goMenuPage('/pages/users/user_money/index')">
						<text class="num">{{userInfo.now_money || 0}}</text>
						<view class="txt">剩余余额</view>
					</view>
					<view class="num-item" @click="goMenuPage('/pages/users/user_integral/index')">
						<text class="num">{{userInfo.integral || 0}}</text>
						<view class="txt">积分</view>
					</view>
					<view class="num-item" @click="goMenuPage('/pages/users/bonus/index')">
						<text class="num">{{bonus || 0}}</text>
						<view class="txt">优惠券</view>
					</view>
				</view>
				<view class="sign" @click="goSignIn">签到</view>
			</view>
			<view class="order-wrapper">

				<view class="order-hd flex">
					<view class="left">订单中心</view>
					<navigator class="right flex" hover-class="none" url="/pages/users/order_list/index" open-type="navigate">
						查看全部
						<text class="iconfont icon-xiangyou"></text>
					</navigator>
				</view>
				<!-- 买入卖出 -->
				<view class="order-con">
					<view class="order-con-item">
						<text>{{buy}}</text>
						<text>本月买入（元）</text>
					</view>
					<view class="order-con-item">
						<text>{{sell}}</text>
						<text>本月卖出（元）</text>
					</view>
				</view>

				<view class="order-bd">
					<block v-for="(item,index) in orderMenu" :key="index">
						<view class="order-item" @click="switchOrder(item.url,item.title)">
							<view class="pic">
								<image :src="item.img" mode=""></image>
								<text class="order-status-num" v-if="item.num > 0">{{ item.num }}</text>
							</view>
							<view class="txt">{{item.title}}</view>
						</view>
					</block>
				</view>

			</view>
		</view>
		<!-- 轮播 -->
		<!-- <view class="slider-wrapper" v-if="imgUrls.length>0">
			<swiper indicator-dots="true" :autoplay="autoplay" :circular="circular" :interval="interval" :duration="duration"
			 indicator-color="rgba(255,255,255,0.6)" indicator-active-color="#fff">
				<block v-for="(item,index) in imgUrls" :key="index">
					<swiper-item>
						<navigator :url='item.url' class='slide-navigator acea-row row-between-wrapper' hover-class='none'>
							<image :src="item.pic" class="slide-image"></image>
						</navigator>
					</swiper-item>
				</block>
			</swiper>
		</view> -->
		<!-- 会员菜单 -->
		<view class="user-menus" style="margin-top: 20rpx;">
			<block v-for="(item,index) in MyMenus" :key="index">

				<navigator class="item" :url="item.url" hover-class="none" v-if="item.url!='#' && item.url!='/pages/service/index'">
					<view class="left">
						<image :src="item.pic"></image>
						<text>{{item.name}}</text>
					</view>
					<view class="iconfont icon-xiangyou"></view>
				</navigator>
			</block>
			<!-- #ifdef H5 -->
			<navigator class="item" url="/pages/customer_list/chat" hover-class="none">
				<view class="left">
					<image src="/static/images/user_menu08.png"></image>
					<text>联系客服</text>
				</view>
				<view class="iconfont icon-xiangyou"></view>
			</navigator>
			<!-- #endif -->
			<!-- #ifdef MP -->
			<button class="item" open-type='contact' hover-class='none'>
				<view class="left">
					<image src="/static/images/user_menu08.png"></image>
					<text>联系客服</text>
				</view>
				<view class="iconfont icon-xiangyou"></view>
			</button>
			<!-- #endif -->
		</view>
		<!--		<img src="/static/images/support.png" alt="" class='support'>-->
		<view style="height: 50rpx;"></view>
		<!-- #ifdef MP -->
		<!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
		<!-- #endif -->

		<!-- 遮罩层 -->
		<view class="select_type" v-show="isShowSelectType" @click="downSelectType(false)">
			<view class="select_type_item">
				<!-- 已选择的内容 -->
				<view class="select_cont">
					<view class="select_area" @click.stop="selectAreaChilds">
						{{currAreaName}}
						<image class='select_area_img' :class="isShowSelectChild? 'area_img_turn':'area_img_down'" src="../../static/images/xiala.png"
						 mode=""></image>
					</view>

					<scroll-view class="area_list" scroll-y v-show="isShowSelectChild">
						<view class="area_item" v-for="(item,index) in allAreaNameList" :key='index' @click.stop="currentAreaName(item)">{{item.merger_name}}</view>
					</scroll-view>
				</view>
				<view class="select_xz"> 
					<text @click.stop="downSelectType(false)">取消</text>
					<text @click.stop="downSelectType(true)">确定</text>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		getBuyAndSell,
		bonus,
		getMenuList,
		getUserInfo,
		setVisit,
		updateUserInfo,
		getMyAreaName,
		getMyAllAreaName,
		switchMyThisArea
	} from '@/api/user.js';
	import {
		getMyOrder, 
		getMyOrders
	} from '../../api/sellOrder.js'
	import {
		wechatAuthV2
	} from '@/api/public.js'
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	// #ifdef H5
	import Auth from '@/libs/wechat';
	// #endif
	const app = getApp();
	export default {
		components: {
			// #ifdef MP
			authorize
			// #endif
		},
		computed: mapGetters(['isLogin']),
		data() {
			return {
				//  买入
				buy: 0,
				// 卖出
				sell: 0,
				// 开发奖金
				bonus: 0,
				orderMenu: [{
						img: '/static/images/order1.png',
						title: '买入订单', 
						url: '/pages/users/order_list/index'
					},
					{
						img: '/static/images/order2.png',
						title: '卖出订单',
						url: '/pages/users/sellorder_list/index'
					},
					{
						img: '/static/images/tongji.png',
						title: '资产统计',
						url: '/pages/user/profit/index'
					},
					{
						img: '/static/images/yuncang.png',
						title: '我的云仓',
						url: ' '
					} 
				],
				imgUrls: [],
				autoplay: true,
				circular: true,
				interval: 3000,
				duration: 500,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				orderStatusNum: {},
				userInfo: {},
				MyMenus: [],
				isShowSelectType: false,
				isShowSelectChild: false,
				// 当前选中的地址id
				currAreaId: null,
				// 代理地区
				mergerName: '',
				// 弹框默认文字
				currAreaName: '点击可切换新的代理地区',
				// 全部代理地区列表
				allAreaNameList: [],
				// 是否展示切换功能
				isShowChangeArea: '',
				// #ifdef H5
				isWeixin: Auth.isWeixin()
				//#endif
			}
		},
		onLoad(options) {
			let that = this; 
			that.$set(that, 'MyMenus', app.globalData.MyMenus);
			if (that.isLogin == false) {
				// #ifdef H5 || APP-PLUS || MP
				toLogin()
				// #endif 
			} 
			// #ifdef H5
			if (options.code) {
				let spread = app.globalData.spid ? app.globalData.spid : '';
				let gift = app.globalData.gift ? app.globalData.gift : '';
				wechatAuthV2(options.code, spread, gift).then(res => {
					location.href = decodeURIComponent(
						decodeURIComponent(options.back_url)
					)
				})
			}
			// #endif


		},
		onShow: function() { 
			let that = this;
			if (that.isLogin) {
				this.getUserInfo();
				this.getMyMenus();
				this.setVisit();
				this.getMyAreaName();
				this.getBonus();
				this.buyAndSell();
			};
		},
		methods: {
			// 本月买入卖出
			buyAndSell() {
				var that = this
				getBuyAndSell().then(res => {
					that.$set(that, 'buy', res.data.result.buy)
					that.$set(that, 'sell', res.data.result.sell)
				}).catch(res => {

				})
			},
			//  获取开发奖金
			getBonus() {
				var that = this
				bonus().then(res => {
					that.$set(that, 'bonus', res.data.result.money)
					console.log(that.bonus)
				}).catch(res => {

				})
			},
			getWechatuserinfo() {
				//#ifdef H5
				Auth.isWeixin() && Auth.oAuth('snsapi_userinfo', '/pages/user/index');
				//#endif
			},
			getRoutineUserInfo(e) {
				updateUserInfo({
					userInfo: e.detail.userInfo
				}).then(res => {
					this.getUserInfo();
					return this.$util.Tips('更新用户信息成功');
				}).catch(res => {

				})
				console.log(e);
			},
			// 记录会员访问
			setVisit() {
				setVisit({
					url: '/pages/user/index'
				}).then(res => {})
			},
			// 打开授权
			openAuto() {      
				uni.removeStorageSync('authIng'); 
				toLogin();   
			},
			// 授权回调
			onLoadFun() {
				this.getUserInfo();
				this.getMyMenus();
				this.setVisit();
			}, 
			Setting: function() {
				uni.openSetting({
					success: function(res) {
						console.log(res.authSetting)
					}
				});
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			// 绑定手机
			bindPhone() {
				uni.navigateTo({
					url: '/pages/users/user_phone/index'
				})
			},
			/**
			 * 获取个人用户信息
			 */
			getUserInfo: function() {
				let that = this;
				getUserInfo().then(res => { 
					that.userInfo = res.data
					that.$store.commit("SETUID", res.data.uid);
					// that.orderMenu.forEach((item, index) => {
					// 	switch (item.title) {
					// 		case '待付款':
					// 			item.num = res.data.orderStatusNum.unpaid_count
					// 			break
					// 		case '待发货':
					// 			item.num = res.data.orderStatusNum.unshipped_count
					// 			break
					// 		case '待收货':
					// 			item.num = res.data.orderStatusNum.received_count
					// 			break
					// 		case '待评价':
					// 			item.num = res.data.orderStatusNum.evaluated_count
					// 			break
					// 		case '售后/退款':
					// 			item.num = res.data.orderStatusNum.refund_count
					// 			break
					// 	}
					// })
				});
			},
			/**
			 *
			 * 获取个人中心图标
			 */
			getMyMenus: function() {
				let that = this;
				if (this.MyMenus.length) return;
				getMenuList().then(res => { 
					console.log(res)
					that.$set(that, 'MyMenus', res.data.routine_my_menus);
					this.imgUrls = res.data.routine_my_banner
				});
			},
			// 编辑页面
			goEdit() {
				if (this.isLogin == false) {
					toLogin();
				} else {
					uni.navigateTo({
						url: '/pages/users/user_info/index'
					})
				}

			},
			// 签到
			goSignIn() {
				console.log('签到事件触发了')
				uni.navigateTo({
					url: '/pages/users/user_sgin/index'
				})
			},
			// goMenuPage
			goMenuPage(url) {
				if (this.isLogin) {
					uni.navigateTo({
						url
					})
				} else {
					// #ifdef MP
					this.openAuto()
					// #endif
				}
			},
			// 获取代理地区
			getMyAreaName() {
				getMyAreaName().then(res => {
					if (res.data.status == 200) {
						if (Array.isArray(res.data.result)) {
							this.mergerName = res.data.result[0].name
							this.isShowChangeArea = res.data.result[0].count > 1
						} else {
							this.mergerName = res.data.result.name
							this.isShowChangeArea = res.data.result.count > 1
						}
					} else {
						return this.$util.Tips({
							title: res.msg,
						});
					}
				})
			},
			// 获取全部代理地区
			getMyAllAreaName() {
				getMyAllAreaName().then(res => {
					if (res.data.status == 200) {
						this.allAreaNameList = res.data.result
					} else {
						return this.$util.Tips({
							title: res.msg,
						});
					}
				})
			},
			// 切换代理地区
			changeArea() {
				this.isShowSelectType = true
				this.getMyAllAreaName()
			},
			// 关闭弹框
			downSelectType(type) {
				if (type) {
					switchMyThisArea(this.currAreaId).then(res => {
						if (res.data.status == 200) {
							this.$util.Tips({
								title: res.msg
							});
							this.isShowSelectType = false
							this.getMyAreaName()
						}
					})
				} else {
					this.isShowSelectType = false
				}
			},
			// 开启关闭二级区域选择菜单
			selectAreaChilds() {
				this.isShowSelectChild = !this.isShowSelectChild
			},
			// 重新选择区域
			currentAreaName(info) {
				this.currAreaName = info.merger_name
				this.currAreaId = info.id
				this.isShowSelectChild = false
			},
			// 切换买入和卖出
			switchOrder(url,title) {  
				if(title == '我的云仓'){
						this.$util.Tips({ title: '暂未开放'})
				} else if (title === '卖出订单'){
					this.getMyOrder()
					return 
				}else{
					uni.navigateTo({
						url 
					})
				} 
			},
			// 获取卖出订单
			getMyOrder() {
				let that = this
				getMyOrder().then(res => { 
					if (res.data.status == 200) {  
						if(res.data.result.data.length == 0){
							 this.$util.Tips({
								title: '暂无数据'
							}); 
						}
						uni.navigateTo({
							url: '../users/sellorder_list/index'
						})
					} else {
						 this.$util.Tips({
							title: res.msg
						});  
					}
				})
			}, 
		}
	}
</script>

<style lang="scss">
	.new-users {
		.head {
			padding-top: 30rpx;
			background: #fff;

			.user-card {
				position: relative;
				width: 710rpx;
				height: 340rpx;
				margin: 0 auto;
				padding: 35rpx 28rpx;
				background: linear-gradient(90deg, $bg-star 0%, $bg-end 100%);
				box-shadow: 0px 10rpx 20rpx 0px rgba(255, 2, 0, 0.2);
				border-radius: 24rpx;

				.bg {
					position: absolute;
					left: 0;
					top: 0;
					width: 100%;
					height: 100%;
					background-image: url('~@/static/images/user_bg.png');
					background-size: 100% 100%;
				}

				.user-info {
					z-index: 20;
					position: relative;
					display: flex;

					.bntImg {
						width: 120rpx;
						height: 120rpx;
						border-radius: 50%;
						text-align: center;
						line-height: 120rpx;
						background-color: unset;
						position: relative;

						.avatarName {
							font-size: 16rpx;
							color: #fff;
							text-align: center;
							background-color: rgba(0, 0, 0, 0.6);
							height: 37rpx;
							line-height: 37rpx;
							position: absolute;
							bottom: 0;
							left: 0;
							width: 100%;
						}
					}

					.avatar {
						width: 120rpx;
						height: 120rpx;
						border-radius: 50%;
					}

					.info {
						flex: 1;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						margin-left: 20rpx;
						padding: 15rpx 0;

						.name {
							display: flex;
							align-items: center;
							color: #fff;
							font-size: 31rpx;

							.vip {
								display: flex;
								align-items: center;
								height: 36rpx;
								padding: 0 20rpx;
								background: rgba(0, 0, 0, 0.2);
								border-radius: 18px;
								font-size: 20rpx;
								margin-left: 12rpx;

								image {
									width: 27rpx;
									height: 27rpx;
								}
							}
						}

						.num {
							display: flex;
							align-items: center;
							font-size: 26rpx;
							color: rgba(255, 255, 255, 0.6);

							image {
								width: 22rpx;
								height: 23rpx;
								margin-left: 20rpx;
							}
						}
					}

					.change_area {
						position: absolute;
						top: 40rpx;
						left: 70%;
						width: 120rpx;
						text-align: center;
						height: 120rpx;
						color: #fbb38c;

						.area_img {
							display: block;
							margin: 0 auto;
							width: 55rpx;
							height: 50rpx;
						}
					}
				}

				.num-wrapper {
					z-index: 30;
					position: relative;
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 35rpx;
					// padding: 0 47rpx;
					color: #fff;

					.num-item {
						width: 33.33%;
						text-align: center;

						.num {
							font-size: 42rpx;
							font-weight: bold;
						}

						.txt {
							margin-top: 8rpx;
							font-size: 26rpx;
							color: rgba(255, 255, 255, 0.6);
						}
					}
				}

				.sign {
					z-index: 200;
					position: absolute;
					right: -12rpx;
					top: 80rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					width: 120rpx;
					height: 60rpx;
					background: linear-gradient(90deg, rgba(255, 225, 87, 1) 0%, rgba(238, 193, 15, 1) 100%);
					border-radius: 29rpx 4rpx 4rpx 29rpx;
					color: #282828;
					font-size: 28rpx;
					font-weight: bold;
				}
			}

			.order-wrapper {
				.order-hd {
					justify-content: space-between;
					padding: 0 20rpx 28rpx;
					margin-top: 33rpx;
					border-bottom: 1px solid #F5F5F5;
					font-size: 30rpx;
					color: #282828;

					.right {
						align-items: center;
						color: #666666;
						font-size: 26rpx;

						.icon-xiangyou {
							margin-left: 5rpx;
							margin-top: 6rpx;
							font-size: 26rpx;
						}
					}
				}

				.order-con {
					padding: 30rpx 0;
					display: flex;
					height: 160rpx;
					font-size: 26rpx;
					border-bottom: 2rpx solid #ededed;

					.order-con-item {
						text-align: center;
						flex: 1;
						line-height: 50rpx;

						& text:nth-child(1) {
							display: block;
							font-weight: 700;
							color: #ab3e4b;
						}
					}

					& .order-con-item:nth-child(1) {
						border-right: 1px solid #efefef;
					}
				}

				.order-bd {
					display: flex;
					padding: 0 24rpx;

					.order-item {
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						width: 30%;
						height: 160rpx;

						.pic {
							position: relative;
							text-align: center;

							image {
								width: 48rpx;
								height: 48rpx;
							}
						}

						.txt {
							margin-top: 15rpx;
							font-size: 26rpx;
							color: #454545;
						}
					}
				}
			}
		}

		.slider-wrapper {
			margin: 20rpx 0;
			height: 130rpx;

			swiper,
			swiper-item {
				height: 100%;
			}

			image {
				width: 100%;
				height: 130rpx;
			}
		}

		.user-menus {
			background-color: #fff;

			.item {
				position: relative;
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 90rpx;
				padding: 0 30rpx;

				.left {
					display: flex;
					align-items: center;

					image {
						width: 46rpx;
						height: 46rpx;
						margin-right: 25rpx;
					}
				}

				&::before {
					content: ' ';
					position: absolute;
					right: 0;
					bottom: 0;
					width: 655rpx;
					height: 1px;
					background-color: #EEEEEE;
				}

				&:last-child::before {
					display: none;
				}
			}

			button {
				font-size: 28rpx;
			}
		}

		.phone {
			color: #fff;
		}

		.order-status-num {

			min-width: 12rpx;
			background-color: #fff;
			color: #ee5a52;
			border-radius: 15px;
			position: absolute;
			right: -14rpx;
			top: -15rpx;
			font-size: 20rpx;
			padding: 0 8rpx;
			border: 1px solid #ee5a52;
		}

		.support {
			width: 219rpx;
			height: 74rpx;
			margin: 54rpx auto;
			display: block;
		}

		.select_type {
			position: fixed;
			top: 0;
			width: 100%;
			height: 100%;
			background: rgba(0, 0, 0, 0.5);

			.select_type_item {
				padding: 40rpx 20rpx;
				position: fixed;
				z-index: 100;
				top: 50%;
				left: 50%;
				transform: translate(-50%, -50%);
				width: 80%;
				border-radius: 20rpx;
				background-color: #f5f5f5;
			}

			.select_cont {
				position: relative;
				height: 500rpx;
				font-size: 30rpx;
				line-height: 100rpx;
				color: #9d9696;

				.select_area {
					padding-left: 30rpx;
					border: 1px solid #9d9696;
					border-radius: 20rpx;

					.select_area_img {
						display: inline-block;
						margin-left: 80rpx;
						width: 50rpx;
						height: 50rpx;
						vertical-align: middle;
					}

					.area_img_turn {
						transform: rotate(180deg);
						transition: all .5s;
					}

					.area_img_down {
						transform: rotate(0deg);
						transition: all .5s;
					}
				}

				.area_list {
					margin-top: 10rpx;
					height: 380rpx;
					border-radius: 20rpx;
					border: 1px solid #9d9696;

					.area_item {
						margin: 0 20rpx;
						height: 80rpx;
						line-height: 80rpx;
						border-bottom: 1rpx solid #9d9696;
					}
				}
			}

			.select_xz {
				width: 100%;
				display: flex;

				text {
					flex: 1;
					margin: 0 20rpx;
					height: 80rpx;
					line-height: 80rpx;
					text-align: center;
					border-radius: 20rpx;
					color: #fff;
					background-image: linear-gradient(to right, #f62c2c, #f96829);
				}

				& text:last-child {
					background: #22ac38;
				}
			}
		}
	}
</style>
